// pages/index/law_service/leave_messages/leave_messages.js
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from  "../../../../miniprogram_npm/@vant/weapp/dialog/dialog"

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    user_id: "",
    list:[],
    isLoading: true,
    isRefresh: false,
    pageNumber: 1,
    bottomId: 'item-0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
      user_id: app.globalData.userInfo.applet_user_id
    })
  },

  /**
   * 下拉刷新
   * @param e
   */
  onRefresh: function (e) {
    this.setData({
      isRefresh: true
    })
    this.getLeaveMessages()
  },

  /**
   * 下拉加载更多
   * @param e
   */
  onUpper: function (e) {
    this.data.pageNumber++
    this.getLeaveMessages()
  },

  /**
   * 上拉加载更多
   * @param e
   */
  onLower: function (e) {
    this.data.pageNumber++
    this.getLeaveMessages()
  },

  /**
   * 获取留言记录
   */
  getLeaveMessages: function (){
    this.setData({
      isLoading: true
    })
    Toast.loading({
      duration: 0,
      message: "加载中..."
    })
    http.get("information_reply/getReplyAndMessage",{
      post_id: this.data.id,
      reply_user_id: app.globalData.userInfo.applet_user_id,
      page_number: this.data.pageNumber,
      page_size: 10
    }).then(res => {
      Toast.clear()
      if (res.data.code === http.REQUEST_OK){
        let list = this.data.list
        if (res.data.data.list.length > 0) {
          res.data.data.list.forEach(item => {
            item.isMine = false
            if (item.reply_user_id == this.data.user_id) {
              item.isMine = true
            }
            list.unshift(item)
            log.e(item.isMine)
          })
        } else {
          Toast.fail("没有更多数据了")
          if (this.data.pageNumber > 1) {
            this.data.pageNumber--
          }
        }
        this.setData({
          list: list
        })
        //判断显示位置
        if (this.data.pageNumber == 1) {
          this.setData({
            bottomId: 'item-' + (this.data.list.length - 1)
          })
        } else {
          this.setData({
            bottomId: 'item-' + (res.data.data.list.length - 1)
          })
        }
      }else {
        Dialog.alert({
          title: "获取数据失败",
          message: res.data.msg
        }).then(() => {  })
        if (this.data.pageNumber > 1) {
          this.data.pageNumber--
        }
      }
      this.setData({
        isRefresh: false,
        isLoading: false
      })
    }, err => {
      setTimeout(() => {
        Toast.clear()
      },100)
      if (this.data.pageNumber > 1) {
        this.data.pageNumber--
      }
      Dialog.alert({
        title: "获取数据失败",
        message: "网络异常，请稍后再试！"
      }).then(() => {  })
      this.setData({
        isRefresh: false,
        isLoading: false
      })
    })
  },

  /**
   * 删除留言
   * @param e
   */
  onLeaveMessagesDelete: function (e) {
    const id = e.currentTarget.dataset.id
    Dialog.confirm({
      title: "温馨提示",
      message: "确定要删除该留言吗？"
    }).then(() => {
      Toast.loading({
        duration: 0,
        message: "删除中..."
      })
      http.putJson("information_reply/del",{
        id: id
      }).then(res => {
        if (res.data.code === http.REQUEST_OK){
          Toast.success("删除成功")
          this.getLeaveMessages()
        }else {
          Toast.fail(res.data.msg)
        }
      }, err => {
        Toast.fail("网络异常")
      })
    }).catch(() => {
      // on cancel
    })
  },

  /**
   * 我要留言
   */
  onAddLeaveMessages: function (){
    wx.navigateTo({
      url: "./add/add?id=" + this.data.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.pageNumber = 1
    this.data.list = []
    this.getLeaveMessages()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})